package edu.biwu.dao.impl;

import edu.biwu.dao.ProductDao;
import edu.biwu.po.Product;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanListHandler;
import org.apache.commons.dbutils.handlers.MapListHandler;

import java.sql.SQLException;
import java.util.List;

/**
 * @author 王俊彪
 * @version 1.0
 */
public class ProductDaoImpl implements ProductDao {

    /*将来交给spring 去注入*/
    private QueryRunner queryRunner;
    @Override
    public void updateProduct(Product product) {
        try {
            queryRunner.update("UPDATE spring.product SET pname=?,price=?,category=? WHERE " +
                    " pid = ?",product.getPname(),product.getPrice(),product.getCategory(),product.getPid());

        } catch (SQLException e) {
            throw new RuntimeException(e.getMessage());
        }
    }

    @Override
    public List<Product> findAll() {
        try {
            return queryRunner.query("SELECT * FROM spring.product",
                    new BeanListHandler<Product>(Product.class));
        } catch (SQLException e) {
            throw new RuntimeException(e.getMessage());
        }

    }

    public ProductDaoImpl(QueryRunner queryRunner) {
        this.queryRunner = queryRunner;
    }

    public ProductDaoImpl() {
    }

    public void setQueryRunner(QueryRunner queryRunner) {
        this.queryRunner = queryRunner;
    }
}
